package function

import ensemble.samples.custom.sqlformat.javafx.util.CsvUtils
import ensemble.samples.custom.sqlformat.javafx.util.MybatisSqlFormat
import ensemble.samples.custom.sqlformat.javafx.util.SqlFormatterUtils
import javafx.scene.input.Clipboard
import javafx.scene.input.ClipboardContent

import java.util.concurrent.Executors
import java.util.stream.Collectors

class common {

    static def addCommaButton = { it.replaceAll("\n", ",\n") }
    static def addQuotationButton = { "'" + it.replaceAll("\n", "',\n'") + "'" }
    static def addDoubleQuotationButton = { "\"" + it.replaceAll("\n", "\",\n\"") + "\"" }
    static def sqlFormat2Button = { SqlFormatterUtils.format(it) }
    static def csv2Excel = { CsvUtils.csv2Excel(it) }
    static def mybatisSqlFormatButton = { SqlFormatterUtils.format(MybatisSqlFormat.getSqlFromStr(it)) }
    static def mybatisSqlOracleFormatButton = {
        SqlFormatterUtils.format(MybatisSqlFormat.getSqlFromStr(it, MybatisSqlFormat.DATABASE_TYPE_ORACLE))
    }
    static def filterRepeatButton = {
        new LinkedHashSet<>(toList(it)).join(NEW_LINE)
    }

    static def trimButton = {
        toList(it).collect({ e -> e.trim() }).findAll({ e -> !e.isEmpty() }).join(NEW_LINE)
    }

    static def sortButton = {
        List<String> list = toList(it)
        list.sort(Comparator.naturalOrder())
        return list.join(NEW_LINE)
    }

    static def sortReverseButton = {
        List<String> list = toList(it)
        list.sort(Comparator.reverseOrder())
        return list.join(NEW_LINE)
    }

    static String findRepeatButton(String i) {
        List<String> list = toList(i);
        Set<String> set = new LinkedHashSet<>(list.size());
        List<String> repeat = new ArrayList<>();
        for (final String s : list) {
            if (!set.add(s)) {
                repeat.add(s);
            }
        }
        return repeat.join(NEW_LINE)
    }

    static String copyButton(String i) {
        Clipboard systemClipboard = Clipboard.getSystemClipboard()
        ClipboardContent content = new ClipboardContent()
        content.putString(i)
        systemClipboard.setContent(content)
        return null
    }

    static String clear(String i) {
        return ""
    }

    static String timestampButton(String i) {
        String f = "yyyy-MM-dd HH:mm:ss.SSS"
        ArrayList<String> is = toList(i)
        List<String> rList = is.stream().filter({!it.isEmpty()}).map({e ->
            e = e.trim()
            if (e.isLong()) {
                if (e.length() == 10) {
                    e = e + '000'
                }
                return e + " " + new Date(Long.parseLong(e)).format(f)
            }
            try {
                Date t = new Date().parse(f, e)
                return t.getTime() + " " + t.format(f)
            } catch (Exception ignored) {
//                ignored.printStackTrace()
                return e
            }
        }).collect(Collectors.toList())
        String nowStart = "now : "
        boolean hasNow = rList.stream().anyMatch({it.contains(nowStart)})
        if (hasNow) {
            rList.remove(0)
        }
        long e = System.currentTimeMillis()
        rList.add(0, nowStart + e + " " + new Date(e).format(f))
        return rList.join(NEW_LINE)
    }

    public static final String NEW_LINE = "\n";

    static private List<String> toList(String i) {
        return Arrays.asList(i.split(NEW_LINE));
    }


}